package cn.iocoder.yudao.boot.web.controller;


import cn.iocoder.yudao.boot.common.pojo.R;
import cn.iocoder.yudao.boot.jpa.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

public abstract class JpaController<S extends BaseService<T, ID>, T, ID> {

    @Autowired
    protected S service;

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody T t){
        return R.ok(service.save(t));
    }

    @PutMapping("/update")
    public R<Boolean> update(@RequestBody T t){
        //service.updateById(t);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    public R<T> delete(@PathVariable("id") ID id){
        service.delete(id);
        return R.ok();
    }

    @GetMapping("/get/{id}")
    public R<T> get(@PathVariable("id") ID id){
        Optional<T> optional = service.get(id);
        return R.ok(optional.orElse(null));
    }

    @GetMapping("/list")
    public R<List<T>> list(){
        List<T> list = service.list();
        return R.ok(list);
    }

    @GetMapping("/page")
    public R<Page<T>> page(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        return R.ok(service.page(page, page));
    }
}
